/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			pglnpattscale.cc
  \date			Aug 2012
  \author		TNick

  \brief		Contains the implementation of PgLnPattScale class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<cpg/cpg.h>

#include	<cpg/logic/cpgdoc.h>
#include	<cpg/logic/layer/pglayglob.h>
#include	<cpg/logic/layer/pglayspc.h>
#include	<cpg/logic/pgctx.h>

#include	"pglnpattscale.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace cpg;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
PgLnPattScale::PgLnPattScale				( CpgDoc * doc )
	: PgCharacteristic( doc )
{
	scale_ = 1;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgLnPattScale::~PgLnPattScale				( void )
{
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PgLnPattScale *			PgLnPattScale::clone				( CpgDoc * destin, int & idx )
{
	Q_ASSERT( destin != NULL );
	PgLnPattScale * ret = new PgLnPattScale( destin );

	ret->scale_ = scale_;
	ret->setName( name() );

	destin->addLnPattScale( ret );
	idx = destin->lnPattScaleCount() - 1;

	return ret;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool				PgLnPattScale::isIdValid		( CpgDoc * doc, int id )
{
	if ( id >= 0 )
	{
		if ( id < doc->lnPattScaleCount() )
			return true;
		return false;
	}
	else
	{
		switch		( id )	{
		case	SCALE_1:
		case	BYLAYER:
		case	BYBLOCK:
			return true;
		default:
			return false;
		}
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
qreal					PgLnPattScale::explicitScale		(
		PgCtx * ctx, qreal id )
{
	qreal		ret;

	for ( ;; )
	{
		if ( id == BYLAYER )
		{
			if ( ctx->layer_ == NULL )
			{
				Q_ASSERT( false );
				id = 1;
			}
			else
			{
				id = ctx->layer_->linePattScl();
				if ( id == BYLAYER )
				{
					Q_ASSERT( false );
					id = 1;
				}
			}
			continue;
		}
		else if ( id == BYBLOCK )
		{
			if ( ctx->block_def_ == NULL )
			{
				Q_ASSERT( false );
				id = 1;
				continue;
			}
			else
			{
				/** @todo implement
				id = blk->linePattScl();
				blk = blk->block();*/
				Q_ASSERT( false );
				ret = 1;
				break;
			}
		}
		else if ( id == SCALE_1 )
		{
			ret = 1;
		}
		else if ( id < 0 )
		{
			Q_ASSERT( false );
			ret = 1;
		}
		else
		{
			if ( id < ctx->document_->lnPattScaleCount() )
			{
				ret = ctx->document_->lnPattScale( id )->scale();
			}
			else
			{
				Q_ASSERT( false );
				ret = 1;
			}
		}
		break;
	}
	return ret;

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					PgLnPattScale::asXML		(
		PgCtx * ctx, qreal id, QDomElement & dest )
{
	/*
	dest.setAttribute( "pg-pattern-scale", id );
	dest.setAttribute(
				"pg-pattern-scale-explicit",
				explicitScale( ctx, id )
				);
	*/
	/** @todo asXML */
	Q_UNUSED( ctx );
	Q_UNUSED( id );
	Q_UNUSED( dest );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString					PgLnPattScale::nameForId	( PgLnPattScale::Id id )
{
	switch		( id )	{
	case	SCALE_1:
		return QObject::tr( "identity" );
	case	BYLAYER:
		return QObject::tr( "by layer" );
	case	BYBLOCK:
		return QObject::tr( "by block" );
	default:
		return QString();
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					PgLnPattScale::nameAndId	(
		QStringList & name_list, QList<int> & id_list )
{
#	define	nameAndId_helper(i)	\
	id_list.append( i ); name_list.append( nameForId( i ) );

	nameAndId_helper( SCALE_1 );
	nameAndId_helper( BYLAYER );
	nameAndId_helper( BYBLOCK );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString					PgLnPattScale::asString		(
		CpgDoc * doc, int id )
{
	if ( id >= 0 )
	{
		if ( id < doc->lnPattScaleCount() )
			return doc->lnPattScale( id )->name();
		return QString();
	}
	else
	{
		return nameForId( (Id)id );
	}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
int						PgLnPattScale::copy			(
		CpgDoc * source, CpgDoc * destin, int value )
{
	Q_ASSERT( source != NULL );
	Q_ASSERT( destin != NULL );
	Q_ASSERT( isIdValid( source, value ) );

	/* can we directly use this value? */
	if ( source == destin )
		return value;

	if ( value >= 0 )
	{
		int i;
		/*PgLnPattScale * new_col = */ source->lnPattScale( value )->clone( destin, i );
		return i;
	}
	else
	{
		return value;
	}
}
/* ========================================================================= */



/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
